;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.KeyCatePurchaseCase", [])
        .controller("KeyCatePurchaseCaseCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            $scope.toolbarConfig = {};

            $scope.tbConf = {
                scrollWidth: 1200,
                tableCol: [
                    {
                        name: "代理商名称",
                        dataField: "dealerName",
                        width: 300,
                    },
                    {
                        name: "重点品类",
                        dataField: "impClass",
                        width: 150,
                    },
                    {
                        name: "任务数",
                        dataField: "taskNum",
                        width: 120,
                    },
                    {
                        name: "月度完成",
                        dataField: "completed",
                        width: 120,
                    },
                    {
                        name: "完成率",
                        dataField: "completedRate",
                        width: 120,
                    }
                ]
            };

            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/impClassTask/exportReport"),
            };

            $scope.condition = {
                isDealer: !$scope.isZHJCH
            };
            $scope.dataList = [];

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.reset = function () {
                $scope.condition = {
                    isDealer: !$scope.isZHJCH
                };
            };

            // 获取报表列表
            $scope.searchData = function (curPage) {
                var url = '/impClassTask/list4Report';
                if (curPage) $scope.pageInfo.curPage = curPage;
                var params = {
                    filters: angular.copy($scope.condition),
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    const dataT = resp.value.list || [];
                    const groups = {};
                    dataT.forEach(g => {
                        g.saleDateShow = $filter("YbDate")(g.saleDate, "yyyy-MM");
                        const groupId = g['area'];
                        groups[groupId] = groups[groupId] || []
                        groups[groupId].push(g)
                    })

                    const dataRes = []
                    Object.keys(groups).forEach(k => {
                        dataRes.push({
                            areaData: groups[k],
                            areaName: k
                        });
                    })

                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = dataRes;
                })
            };

            (function () {
                $scope.searchData();
            })()
        }])
    ;
})(window, window.angular);